import {parseTime} from "@/tool/utils.js";
import {ElMessage} from "element-plus";


export function loginAuth() {
    let user = JSON.parse(localStorage.getItem('healthUser'))
    if (user == null) {
        ElMessage({message: '请先登录', type: 'warning'})
        return 0
    }
    return 1
}

export function intAreaAuth(data, min, max) {
    if (data > max || data < min) {
        ElMessage({message: `值在不合理范围(${min}~${max})`, type: 'error'})
        throw new Error('值在不合理范围')
    }
    return data
}

export function intAuth(data) {
    let number = parseInt(data)
    if (number == null || isNaN(number)) {
        ElMessage({message: '请输入正确值', type: 'error'})
        throw new Error('请输入正确值')
    }
    if (number > 100000 || number < -100000) {
        ElMessage({message: `值在不合理范围`, type: 'error'})
        throw new Error('值在不合理范围')
    }
    return number
}

export function parseTimeToDay(time) {
    const date = new Date(time);
    const now = new Date();
    const diff = now - date; // 差值，单位为毫秒

    const days = Math.floor(diff / (1000 * 60 * 60 * 24)); // 转换为天数

    if (days === 0) {
        return '最近一天内';
    } else if (days === 1) {
        return '昨天';
    } else if (days > 1 && days <= 7) {
        return `${days}天前`;
    } else {
        return parseTime(time, '{y}-{m}-{d}')
    }
}

export function showDictUtil(data, dict) {
    const item = dict.find(item => item.value === data);
    return item ? item.label : 'error';
}